/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 13.Eki.2010, 22:04:30
 */
package view.user;

import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import model.user.User;

/**
 *
 * @author CUNEYT
 */
public class MainFrame extends javax.swing.JFrame {

    private User user = null;

    /** Creates new form MainFrame */
    public MainFrame(User usr) {
        this.user = usr;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainLayeredPane = new javax.swing.JLayeredPane();
        directorView1 = new view.user.DirectorView();
        staffView1 = new view.user.StaffView();
        governorView1 = new view.user.GovernorView();
        adminView1 = new view.user.AdminView();
        mainMenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        changePasswordMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Advert Consultancy Inc.");

        mainLayeredPane.setPreferredSize(null);
        directorView1.setBounds(10, 10, 970, 570);
        mainLayeredPane.add(directorView1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffView1.setBounds(10, 10, 970, 570);
        mainLayeredPane.add(staffView1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        governorView1.setBounds(10, 10, 970, 570);
        mainLayeredPane.add(governorView1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        adminView1.setBounds(20, 10, 716, 432);
        mainLayeredPane.add(adminView1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        fileMenu.setMnemonic('F');
        fileMenu.setText("File");

        changePasswordMenuItem.setMnemonic('C');
        changePasswordMenuItem.setText("Change Password");
        changePasswordMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changePasswordMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(changePasswordMenuItem);

        exitMenuItem.setMnemonic('X');
        exitMenuItem.setText("Exit");
        exitMenuItem.setPreferredSize(new java.awt.Dimension(70, 22));
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        mainMenuBar.add(fileMenu);

        setJMenuBar(mainMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainLayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 1002, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainLayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void changeForePanel(javax.swing.JPanel forePanel) {
        getMainLayeredPane().setLayer(getDirectorView(), 0);
        getDirectorView().setVisible(false);
        getMainLayeredPane().setLayer(getStaffView(), 1);
        getStaffView().setVisible(false);
        getMainLayeredPane().setLayer(getGovernorView(), 2);
        getGovernorView().setVisible(false);
        getMainLayeredPane().setLayer(getAdminView(), 3);
        getAdminView().setVisible(false);
        getLayeredPane().setLayer(forePanel, 0);
        forePanel.setVisible(true);

    }

    public AdminView getAdminView() {
        return adminView1;
    }

    public StaffView getStaffView() {
        return staffView1;
    }

    public void setStaffView(StaffView staffView1) {
        this.staffView1 = staffView1;
    }

    public JLayeredPane getMainLayeredPane() {
        return mainLayeredPane;
    }

    public void setMainLayeredPane(JLayeredPane mainLayeredPane) {
        this.mainLayeredPane = mainLayeredPane;
    }

    public DirectorView getDirectorView() {
        return directorView1;
    }

    public void setDirectorView(DirectorView directorView) {
        this.directorView1 = directorView;
    }

    public GovernorView getGovernorView() {
        return governorView1;
    }

    public void setGovernorView(GovernorView governorView1) {
        this.governorView1 = governorView1;
    }

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(1);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void changePasswordMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changePasswordMenuItemActionPerformed

        char[] current = (JOptionPane.showInputDialog("Current password")).toCharArray();

        boolean match = false;
        try {
            if (current.length == user.getPassword().length) {
                for (int i = 0; i < current.length; i++) {
                    if ((current[i]) != ((user.getPassword())[i])) {
                        throw new Exception("Current password does not match");
                    }
                }
            } else {
                throw new Exception("Current password does not match");
            }

            String newPass1 = JOptionPane.showInputDialog("New password");
            if (newPass1 != null) {
                String newPass2 = JOptionPane.showInputDialog("Repeat new password");
                if (newPass2 != null) {
                    if (!newPass1.equals(newPass2)) {
                        throw new Exception("New password does not match!");
                    } else {
                        user.setPassword(newPass1.toCharArray());
                    }
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
    }//GEN-LAST:event_changePasswordMenuItemActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private view.user.AdminView adminView1;
    private javax.swing.JMenuItem changePasswordMenuItem;
    private view.user.DirectorView directorView1;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private view.user.GovernorView governorView1;
    private javax.swing.JLayeredPane mainLayeredPane;
    private javax.swing.JMenuBar mainMenuBar;
    private view.user.StaffView staffView1;
    // End of variables declaration//GEN-END:variables
}
